/**
 * @Desc 拖拽
 */

export const vDrag = {
  bind: function(el: any, binding: any) {
    let oDiv = el; // 当前元素
    oDiv.onmousedown = function(e: any) {
      // 鼠标按下，计算当前元素距离可视区的距离
      let disX = e.clientX - oDiv.offsetLeft;
      let disY = e.clientY - oDiv.offsetTop;
      // 修改cursor为move
      e.target.style.cursor = "move";

      document.onmousemove = function(e: any) {
        // 通过事件委托，计算移动的距离
        let l = e.clientX - disX;
        let t = e.clientY - disY;
        // 边界位置手工测量
        // console.log(l, t);
        if (l >= 15 && l <= 600 && t >= 30 && t <= 355) {
          oDiv.style.left = l + "px";
          oDiv.style.top = t + "px";
        } else {
          return false;
        }
        // 将此时的位置传出去
        // binding.value({ x: e.pageX, y: e.pageY });
      };
      document.onmouseup = function(e: any) {
        document.onmousemove = null;
        document.onmouseup = null;

        e.target.style.cursor = "default";
      };
    };
  },
};
